package com.aaa.controller;

import com.aaa.pojo.*;
import com.aaa.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/person")
public class PersonController {
    @Autowired
    private ChangeMoneyService changeMoneyService;
    @Autowired
    private PayMoneyService payMoneyService;
    @Autowired
    private TermFinancialService termFinancialService;
    @Autowired
    private FundManagementService fundManagementService;
    @Autowired
    private PersonUserService personUserService;

    @RequestMapping(value = "/getChangeList/{currentPage}/{pageSize}",method = RequestMethod.GET)
    public PageInfo<ChangeMoney> getChangePage(@PathVariable(name = "currentPage") int currentPage, @PathVariable(name = "pageSize") int pageSize){
        return changeMoneyService.getChangeByPage(currentPage,pageSize);
    }
    @RequestMapping(value = "/getPayList/{currentPage}/{pageSize}",method = RequestMethod.GET)
    public PageInfo<PayMoney> getPayPage(@PathVariable(name = "currentPage") int currentPage, @PathVariable(name = "pageSize") int pageSize){
        return payMoneyService.getPayByPage(currentPage,pageSize);
    }
    @RequestMapping(value = "/getTermList/{currentPage}/{pageSize}",method = RequestMethod.GET)
    public PageInfo<TermFinancial> getTermPage(@PathVariable(name = "currentPage") int currentPage, @PathVariable(name = "pageSize") int pageSize){
        return termFinancialService.getPayByPage(currentPage,pageSize);
    }
    @RequestMapping(value = "/getFundList/{currentPage}/{pageSize}",method = RequestMethod.GET)
    public PageInfo<FundManagement> getFundPage(@PathVariable(name = "currentPage") int currentPage, @PathVariable(name = "pageSize") int pageSize){
        return fundManagementService.getPayByPage(currentPage,pageSize);
    }
    @RequestMapping(name="查询单个零钱信息",value = "/selectChangeById")
    public Map<String,Object> selectChangeById(Integer c_id,HttpServletRequest request){
        HttpSession session = request.getSession();
        Integer uid = (Integer) session.getAttribute("uid");
        Map<String,Object> map=new HashMap<>();
        ChangeMoney changeMoney=changeMoneyService.selectById(c_id);
        List<BankNumber> list=changeMoneyService.selectByBid(uid);
        PersonUser personUser=personUserService.selectByUid(uid);
        map.put("change",changeMoney);
        map.put("Invest",changeMoney.getInvestmentHorizon());
        map.put("bank",list);
        map.put("personUser",personUser);
        return map;
    }
    @RequestMapping(name="查询单个工资信息",value = "/selectPayById")
    public Map<String,Object> selectPayById(Integer pay_id,HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        HttpSession session = request.getSession();
        Integer uid = (Integer) session.getAttribute("uid");
        PayMoney payMoney=payMoneyService.selectByPid(pay_id);
        List<BankNumber> list=changeMoneyService.selectByBid(uid);
        PersonUser personUser=personUserService.selectByUid(uid);
        map.put("payMoney",payMoney);
        map.put("Invest",payMoney.getInvestmentHorizon());
        map.put("pro",payMoney.getProductType());
        map.put("bank",list);
        map.put("personUser",personUser);
        return map;
    }
    @RequestMapping(name="查询单个期限信息",value = "/selectTermById")
    public Map<String,Object> selectTermById(Integer t_id,HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        HttpSession session = request.getSession();
        Integer uid = (Integer) session.getAttribute("uid");
        TermFinancial termFinancial=termFinancialService.selectByTid(t_id);
        List<BankNumber> list=changeMoneyService.selectByBid(uid);
        PersonUser personUser=personUserService.selectByUid(uid);
        map.put("termFinancial",termFinancial);
        map.put("Invest",termFinancial.getInvestmentHorizon());
        map.put("bank",list);
        map.put("personUser",personUser);
        return map;
    }
    @RequestMapping(name="查询单个基金信息",value = "/selectFundById")
    public Map<String,Object> selectFundById(Integer f_id,HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        HttpSession session = request.getSession();
        Integer uid = (Integer) session.getAttribute("uid");
        FundManagement fundManagement=fundManagementService.selectByFid(f_id);
        List<BankNumber> list=changeMoneyService.selectByBid(uid);
        PersonUser personUser=personUserService.selectByUid(uid);
        map.put("fundManagement",fundManagement);
        map.put("Invest",fundManagement.getInvestmentHorizon());
        map.put("groups",fundManagement.getGrowthRate());
        map.put("types",fundManagement.getFundType());
        map.put("bank",list);
        map.put("personUser",personUser);
        return map;
    }
    @PostMapping(name="记录零钱理财",value = "/selectChanges")
    public String mychange(Integer c_id, Double amount, String c_name,Integer bank_id,HttpServletRequest request){
        HttpSession session = request.getSession();
        Integer uid = (Integer) session.getAttribute("uid");
        if(changeMoneyService.addChangeMoney(uid,c_id,amount)){
            changeMoneyService.addRecordMoney(amount,c_name);
            if(changeMoneyService.deductionBank(bank_id,amount)){
                return "";
            }
            return "";
        }
        return "";
    }
    @PostMapping(name="记录工资理财",value = "/selectPayMoneys")
    public String myPayMoneys(Integer pay_id, Double amount, String pro_type,Integer bank_id, HttpServletRequest request){
        HttpSession session = request.getSession();
        Integer uid = (Integer) session.getAttribute("uid");
        if(payMoneyService.addPayMoney(uid,pay_id,amount)){
            payMoneyService.addRecordMoney(amount,pro_type);
            if(changeMoneyService.deductionBank(bank_id,amount)){
                return "";
            }
            return "";
        }
        return "";
    }
    @PostMapping(name="记录期限理财",value = "/selectTermMoneys")
    public String myTermMoneys(Integer t_id, Double amount, String t_name,Integer bank_id, HttpServletRequest request){
        HttpSession session = request.getSession();
        Integer uid = (Integer) session.getAttribute("uid");
        if(termFinancialService.addTermMoney(uid,t_id,amount)){
            termFinancialService.addRecordMoney(amount,t_name);
            if(changeMoneyService.deductionBank(bank_id,amount)){
                return "";
            }
            return "";
        }
        return "";
    }
    @PostMapping(name="记录基金理财",value = "/selectFundMoneys")
    public String myFundMoneys(Integer f_id, Double amount, String type_abbreviate,Integer bank_id, HttpServletRequest request){
        HttpSession session = request.getSession();
        Integer uid = (Integer) session.getAttribute("uid");
        if(fundManagementService.addFundMoney(uid,f_id,amount)){
            fundManagementService.addRecordMoney(amount,type_abbreviate);
            if(changeMoneyService.deductionBank(bank_id,amount)){
                return "";
            }
            return "";
        }
        return "";
    }
}
